﻿Public Class UpdateDeleteShopOutletAdmin
    Inherits System.Web.UI.Page
    Public dbmanager As DBManager = New DBManager

    Public outletID As Integer

    'To set masterpage 
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        'Check if user is null
        If (Session("user") IsNot Nothing) Then
            'Retrieve userAcc details from session
            Dim userLoggedIn As userAcc = Session("user")
            'Check if user is admin, assign masterpage respectively
            If userLoggedIn.isAdmin Then
                Me.MasterPageFile = "~/MasterPage/Admin.Master"
            Else
                Me.MasterPageFile = "~/MasterPage/User.Master"
            End If
        Else
            'For anonymous users
            Me.MasterPageFile = "~/MasterPage/User.Master"
            'Enable this line to prevent anonymous users from entering this page
            Response.Redirect("~/Home.aspx")
        End If
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Try
                'Check if there is a report 
                If (Session("ReportID") IsNot Nothing) Then
                    'Check that reportID is not 0 and that user is admin
                    If (Not Session("ReportID") = 0) And Me.MasterPageFile = "/MasterPage/Admin.Master" Then
                        If (Request.QueryString("id") IsNot Nothing) Then
                            Dim fullID = Request.QueryString("id")

                            Dim type = fullID.Substring(0, 1)

                            Select Case type
                                Case "b"

                                    Dim BusinessID = fullID.Substring(1, fullID.Length - 1)
                                    hfBusinessID.Value = BusinessID
                                    hfReportID.Value = Session("ReportID")
                                    ListView1.DataSourceID = "SqlDataSource1"
                                    'get business details by businessID
                                    ListView1.DataBind()
                                    Dim businessData = dbmanager.getBusinessDetailsBID(BusinessID)
                                    tbxBusinessName.Text = businessData(0)
                                    ddlCategory.SelectedItem.Text = businessData(1)
                                    tbxShopURL.Text = businessData(2)
                                    tbxShopDetails.Text = businessData(3)
                                    'get 
                                    Dim businessReportData = dbmanager.retrieveReportDetailsBID(BusinessID)

                                    BusinessDetailPanel.Visible = True
                                    OutletDetailsPanel.Visible = False
                                Case "o"

                                    Dim OutletID = fullID.Substring(1, fullID.Length - 1)
                                    hfOutletsID.Value = OutletID
                                    hfReportID.Value = Session("ReportID")
                                    ListView1.DataSourceID = "SqlDataSource2"
                                    ListView1.DataBind()
                                    'get the outlet details by outletID
                                    Dim OutletData = dbmanager.getOutletDetails(OutletID)
                                    tbxShopOutletName.Text = OutletData(0)
                                    tbxOutletAddress.Text = OutletData(1)
                                    tbxOpeningHours.Text = OutletData(2)
                                    tbxRemarks.Text = OutletData(3)
                                    tbxShopOutletTel.Text = OutletData(4)
                                    BusinessDetailPanel.Visible = False
                                    OutletDetailsPanel.Visible = True

                            End Select

                        Else
                            Response.Redirect("home.aspx")
                        End If
                    Else

                        Response.Redirect("~/Home.aspx")
                    End If
                End If
            Catch ex As Exception
            End Try
            End If

    End Sub
    Protected Sub BtnBusinessUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnBusinessUpdate.Click

     
            'Check that reportID is not 0 and that user is admin
            If (Not Session("ReportID") = 0) And Me.MasterPageFile = "/MasterPage/Admin.Master" Then
            Dim reportID = Session("ReportID")
            If (Request.QueryString("id") IsNot Nothing) Then
                Dim fullID = Request.QueryString("id")
                Dim type = fullID.Substring(0, 1)
                Select Case type
                    Case "b"
                        Dim datecreated As DateTime
                        datecreated = DateTime.Now
                        Dim BusinessID = fullID.Substring(1, fullID.Length - 1)
                        dbmanager.updateBusinessDetails(BusinessID, tbxBusinessName.Text, ddlCategory.SelectedItem.Text, tbxShopURL.Text, tbxShopDetails.Text, datecreated)
                        dbmanager.resolveReport(reportID)
                        Response.Redirect("home.aspx")
                        'update business details 
                        MsgBox("Successfully update business", MsgBoxStyle.OkOnly, "Success")

                End Select
            End If
            End If



    End Sub

    Protected Sub BtnOutletUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnOutletUpdate.Click
        If (Not Session("ReportID") = 0) And Me.MasterPageFile = "/MasterPage/Admin.Master" Then
            Dim reportID = Session("ReportID")
            If (Request.QueryString("id") IsNot Nothing) Then
                Dim fullID = Request.QueryString("id")
                Dim type = fullID.Substring(0, 1)
                Select Case type
                    Case "o"
                        Dim datecreated As DateTime
                        datecreated = DateTime.Now
                        Dim outletID = fullID.Substring(1, fullID.Length - 1)
                        dbmanager.updateOutletDetails(outletID, tbxShopOutletName.Text, tbxOutletAddress.Text, tbxOpeningHours.Text, tbxRemarks.Text, 2, tbxShopOutletTel.Text, datecreated)
                        MsgBox("Successfully update business", MsgBoxStyle.OkOnly, "Success")
                        dbmanager.resolveReport(reportID)
                        Response.Redirect("home.aspx")
                End Select
            End If
        End If

    End Sub

    Protected Sub btnBResolved_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBResolved.Click
        If (Session("ReportID") IsNot Nothing) Then
            Dim reportID = Session("ReportID")
            dbmanager.resolveReport(reportID)
            Session("ReportID") = Nothing
            Response.Redirect(Request.Path)
        End If
    End Sub

    Protected Sub btnOResolved_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOResolved.Click
        If (Session("ReportID") IsNot Nothing) Then
            Dim reportID = Session("ReportID")
            dbmanager.resolveReport(reportID)
            Session("ReportID") = Nothing
            Response.Redirect(Request.Path)
        End If
    End Sub
End Class